package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.R;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserAnswer;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserCourseVo;
import com.localjoycommunity.cpdj.project.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller
 *
 * @author aqie
 * @date 2020-10-29
 */
@RestController
@RequestMapping("/system/users")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;


    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @Log(title = "导出列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增
     */
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改 用户
     */
    @Log(title = "用户修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除 用户
     */
    @Log(title = "用户删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * todo 微心愿认领历史
     */


    /**
     * todo 活动历史
     */


    /**
     * 答题历史
     */
    @GetMapping("/answerHistory")
    public TableDataInfo answerHistory(User user) {
        startPage();
        List<User> list = userService.answerHistory(user);
        return getDataTable(list);
    }

    /**
     * 导出用户答题记录列表
     */
    @Log(title = "用户答题记录", businessType = BusinessType.EXPORT)
    @GetMapping("/exportAnswerHistory")
    public AjaxResult exportAnswerHistory(User user) {
        List<User> list = userService.answerHistory(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "用户答题记录");
    }

    /**
     * 导出活动历史列表
     */
    @Log(title = " 活动", businessType = BusinessType.EXPORT)
    @GetMapping("/exportActivityHistory")
    public AjaxResult exportActivityHistory(User user) {
        List<User> list = userService.answerHistory(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "活动历史");
    }

    /**
     * 用户课程统计
     */
    @GetMapping("statisticCourse")
    public R<List<UserCourseVo>> statisticCourse(){
        startPage();
        List<UserCourseVo> userCourseVos = userService.statisticCourse();
        return R.success(userCourseVos);
    }
}
